package cn.school.workmgr.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.school.basics.dao.BaseSpaceLocationDao;
import cn.school.basics.vo.BaseSpaceLocationVO;
import cn.school.common.utils.SysUserUtils;
import cn.school.workmgr.dao.HygieneDormitoryStatisticsDao;
import cn.school.workmgr.entity.HygieneDormitoryStatistics;
import cn.school.workmgr.service.HygieneDormitoryStatisticsService;
import cn.school.workmgr.vo.StatisticsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HygieneDormitoryStatisticsServiceImpl extends ServiceImpl<HygieneDormitoryStatisticsDao, HygieneDormitoryStatistics> implements HygieneDormitoryStatisticsService {
    @Autowired
    HygieneDormitoryStatisticsDao hygieneDormitoryStatisticsDao;
    @Autowired
    BaseSpaceLocationDao baseSpaceLocationDao;

    /**
     * 新增宿舍卫生统计表
     * @param hygSta
     * @return
     */
    @Override
    public int addHygieneDormitoryStatistics(HygieneDormitoryStatistics hygSta) {
        int insert = hygieneDormitoryStatisticsDao.insert(hygSta);
        return insert;
    }

    /**
     * 根据宿舍id删除宿舍卫生统计表
     * @param dormId 宿舍id
     * @return
     */
    @Override
    public int deleteHygieneDormitoryStatisticsByDormId(long dormId) {
        LambdaQueryWrapper<HygieneDormitoryStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HygieneDormitoryStatistics::getDormId,dormId);
        return hygieneDormitoryStatisticsDao.delete(queryWrapper);
    }

    /**
     * 查询宿舍卫生统计表
     * @param params
     * @return
     */
    @Override
    public IPage<HygieneDormitoryStatistics> pageSql(Map<String, Object> params) {
        // 条件获取
        int pageNum = Integer.parseInt((String) params.get("pageNum"));
        int pageSize = Integer.parseInt((String) params.get("pageSize"));

        String dormName = (String) params.get("dormName"); // 宿舍名称
        String buidId = (String)params.get("buidId");
        Page<HygieneDormitoryStatistics> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        // queryWrapper查询设置
        LambdaQueryWrapper<HygieneDormitoryStatistics> queryWrapper = new LambdaQueryWrapper<>();

//        queryWrapper.isNotNull(HygieneDormitoryStatistics::getDormName).like(HygieneDormitoryStatistics::getDormName,dormName);
//        queryWrapper.isNotNull(HygieneDormitoryStatistics::getBuildId).eq(HygieneDormitoryStatistics::getBuildId, buidId);
        if (StrUtil.isNotEmpty(dormName)) {queryWrapper.like(HygieneDormitoryStatistics::getDormName,dormName);}
        if (StrUtil.isNotEmpty(buidId)) {queryWrapper.eq(HygieneDormitoryStatistics::getBuildId, buidId);}
        //租户
        queryWrapper.eq(HygieneDormitoryStatistics::getTenantId, SysUserUtils.getSysUser().getTenantId());

        List<HygieneDormitoryStatistics> hygieneDormitoryStatisticsList = hygieneDormitoryStatisticsDao.selectList(queryWrapper);
        for(HygieneDormitoryStatistics hyg:hygieneDormitoryStatisticsList){
            BaseSpaceLocationVO baseSpaceLocationVO = baseSpaceLocationDao.selectById(hyg.getBuildId());
            hyg.setBuildName(baseSpaceLocationVO.getLocationName());
        }
        page.setRecords(hygieneDormitoryStatisticsList);
        page.setTotal(hygieneDormitoryStatisticsList.size());
        return page;
    }

    /**
     * 返回要导出的统计表
     * @param id
     * @return
     */
    @Override
    public StatisticsVo getStatisticsVo(long id) {
        HygieneDormitoryStatistics hyg = hygieneDormitoryStatisticsDao.selectById(id);
        BaseSpaceLocationVO baseSpaceLocationVO = baseSpaceLocationDao.selectById(hyg.getBuildId());
        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setBuild(baseSpaceLocationVO.getLocationName());
        statisticsVo.setDormName(hyg.getDormName());
        statisticsVo.setExcellentNum(hyg.getExcellentNum());
        statisticsVo.setGoodNum(hyg.getGoodNum());
        statisticsVo.setInspectNum(hyg.getInspectNum());
        statisticsVo.setUnqualifiedNum(hyg.getUnqualifiedNum());
        return statisticsVo;
    }
}
